package pcb.control.responders 
{
	import com.intel.service.ServiceEvent;
	import com.intel.utils.Debugger;
	import pcb.control.events.LoadDataEvent;
	import pcb.models.events.ModelEvent;
	import pcb.models.LessonStateModel;
	import pcb.models.PlayerSettings;
	import pcb.models.vo.LessonPart;
	import pcb.models.vo.NarrativeVo;
	import pcb.models.vo.PageTypes;
	import pcb.models.vo.Slide;
	
	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class Re_loadLessonXml extends AbstractResponder
	{
		
		public function Re_loadLessonXml() 
		{
			Debugger.traceOut("Re_loadLessonXml constructed");
		}
		
		override public function result(e:ServiceEvent):void 
		{
			Debugger.traceOut("Re_loadLessonXml >>" +e.type + " - " + e.message);
			
			//TODO tmp put this in config.xml command when ready
			//models.playerSettings.playAudio = false;
			
			
			var slideAssetCount:int = 0;
			var data:XML = XML(e.data)
			for each(var lessonPart:XML in data.part) {

				var part:LessonPart = new LessonPart(lessonPart.@type);
				for each(var slide:XML in lessonPart.slides.slide){
					
					//var s:Slide = new Slide(slide.@id, "TODO need a title", slide.@type);
					var s:Slide = new Slide(slide.@id, "", slide.@type);
					s.setPageType(PageTypes.SLIDE);
					s.setContainerId(part.getId());
					
					//daveh - removing the assets folder as the base swf sits inside this
					//var url:String = "assets/" + models.lessonStateModel.lessonId + "/";
					
					var url:String = models.lessonStateModel.lessonId + "/";
					var audioFname:String = slide.instructional.audio;
					if (models.playerSettings.audioQuality != PlayerSettings.DEFAULT_QUALITY_SOUND && models.playerSettings.audioQuality != PlayerSettings.NO_SOUND) {
						
						trace("BEFORE audioFname " + audioFname);
						
						if(audioFname.lastIndexOf(".mp3") > -1) {
							audioFname = audioFname.split("mp3/").join("mp3/" + models.playerSettings.audioQuality + "/");
						}
						
						trace("AFTER audioFname " + audioFname);
						
					}
					
					var instructional:NarrativeVo = new NarrativeVo("s"+slide.@id+"_a"+slideAssetCount, url + audioFname, slide.instructional.text);
					instructional.setOwnerId(slide.@id);
					s.setVO(instructional);
					instructional.addEventListener(ModelEvent.LOADED, instructional_onAssetLoaded);

					//TODO need to check if slide actually has VOText! 
					//DAVEH - check if slide is try and add interactivities here
					//daveh - refactor here to remove as will reuse when non audio version delivered
					if (s.getPedagogy() == Slide.INTERACTIVE || s.getPedagogy() == Slide.ACTIVITY_CARD) {
						s.addInteractions(slide.interactions);
						// no audio so loaded is true for instruct and slide, remove the callback
						instructional.removeEventListener(ModelEvent.LOADED, instructional_onAssetLoaded);
						instructional.setLoaded(true);
						s.setLoaded(true);						
					} else {				
						
						//trace(s.getId() + " is not interactive");
						//trace("models.playerSettings.playAudio " + models.playerSettings.playAudio);
						if (models.playerSettings.playAudio) {
							//trace(s.getId() + " playAudio");
							models.assetsModel.addAsset(instructional.getId(), instructional);
							slideAssetCount++;
							
						}else {
							instructional.removeEventListener(ModelEvent.LOADED, instructional_onAssetLoaded);
							instructional.setLoaded(true);
							s.setLoaded(true);
						}
					}
					
					//now model any other associated IAssets
					//for nodes of type asset
						//s.addAsset(iasset)
						//models.assetsModel.addAsset(iasset.getId(), iasset);
						//slideAssetCount++
					//end for
					
					slideAssetCount = 0;

					part.addSlide(slide.@id, s);
					models.lessonStateModel.addPage(slide.@id, s);
					//trace("ped "+ part.getSlideById(slide.@id).getPedagogy())
					
				}
				
				//-----------------------------------
				//-----------------------------------
				//add 'end of lesson' screen
				var id_eol:String = PageTypes.END_OF_LESSON;
				var sEol:Slide = new Slide(id_eol, "End of Lesson");
				sEol.setPageType(PageTypes.END_OF_LESSON);
				sEol.setVO(null);
				sEol.setLoaded(true);
				sEol.setContainerId(part.getId());
				part.addSlide(id_eol, sEol);
				models.lessonStateModel.addPage(id_eol, sEol, sEol.getPageType());
				//-----------------------------------
				//-----------------------------------
				
				models.lessonStateModel.addLessonPart(part);
				
			}
			
			models.assetsModel.executeQueue();

			models.lessonStateModel.setCurrentLessonTitle(data.name);
			models.lessonStateModel.status = LessonStateModel.INITIALISED;
			
			//dispatch an event to trigger Cmd_loadLessonAssets			
			var ev:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD_LESSON_ASSETS);
			ev.dispatch();
			
			
			
			if (models.playerSettings.autoStartLesson) {
				Debugger.traceOut("Auto-play lesson");
				models.lessonStateModel.startPagePlay();
				
			}
			
			//important for garbage collection
			super.cleanup(e);
		}
		
		private function instructional_onAssetLoaded(e:ModelEvent):void 
		{
			var narVo:NarrativeVo = e.target as NarrativeVo;

			trace("narVo.getOwnerId() loaded " + narVo.getOwnerId());
			Slide(models.lessonStateModel.getLessonPart(0).getSlideById(narVo.getOwnerId())).setLoaded(true);
			
			e.target.removeEventListener(ModelEvent.LOADED, instructional_onAssetLoaded)
		}
		
	}
	
}